-- Taken from Sandbox
-- Modified for my own needs

include("shared.lua")

local drawSB = false
surface.CreateFont("Font_50_akbar", {
    font = "akbar",
	size = 50,
	weight = 100,
	blursize = 0,
	scanlines = 0,
	antialias = true,
	underline = false,
 } )
surface.CreateFont("Font_40_akbar", {
    font = "akbar",
	size = 40,
	weight = 100,
	blursize = 0,
	scanlines = 0,
	antialias = true,
 } )
 surface.CreateFont("Font_20_akbar", {
    font = "akbar",
	size = 20,
	weight = 100,
	blursize = 0,
	scanlines = 0,
	antialias = true,
 } )
 surface.CreateFont("Font_25_akbar", {
    font = "akbar",
	size = 25,
	weight = 100,
	blursize = 0,
	scanlines = 0,
	antialias = true,
 } )
/*---------------------------------------------------------
   Name: gamemode:CreateScoreboard( )
   Desc: Creates/Recreates the scoreboard
---------------------------------------------------------*/
function GM:CreateScoreboard()

	if ( pScoreBoard ) then
	
		pScoreBoard:Remove()
		pScoreBoard = nil
	
	end

	pScoreBoard = vgui.Create( "ScoreBoard" )

end

/*---------------------------------------------------------
   Name: gamemode:ScoreboardShow( )
   Desc: Sets the scoreboard to visible
---------------------------------------------------------*/
function GM:ScoreboardShow()
	
	drawSB = true
	
end


/*---------------------------------------------------------
   Name: gamemode:ScoreboardHide( )
   Desc: Hides the scoreboard
---------------------------------------------------------*/
function GM:ScoreboardHide()
	
	drawSB = false
	
end

OrganizeType = 1 -- How to organize by. 1 is frags, 2 is survival time, 3 is health. 0 is none
width = ScrW()
height = ScrH()
function GM:HUDDrawScoreBoard()
	if drawSB then
		-- Scoreboard box
		draw.RoundedBox(16, width*0.2, height*0.1, width*0.6, height*0.85, Color(0,0,0,math.random(230,255)))
		-- Server name
		draw.SimpleText(GetGlobalString("ServerName"), "Font_50_akbar", width*0.5, height*0.11, Color(0,255,0,math.random(50,200)), TEXT_ALIGN_CENTER)
		-- Gamemode name, author
		draw.SimpleText("Hero Wars", "Font_40_akbar", width*0.5, height*0.16, Color(0,255,0,math.random(50,200)), TEXT_ALIGN_CENTER)
		-- Put version underneath the gamemode name
		draw.SimpleText("("..Version..")", "Font_20_akbar", width*0.4, height*0.21, Color(0,255,0,math.random(50,200)), TEXT_ALIGN_CENTER)
		-- header line
		surface.SetDrawColor(255,255,255,math.random(50,200))
		surface.DrawLine(width*0.2,height*0.24,width*0.8,height*0.24)
		-- vertical line
		surface.SetDrawColor(255,255,255,math.random(50,200))
		surface.DrawLine(width*0.4,height*0.24,width*0.4,height*0.95)
		-- Header stuff
		-- Name
		draw.SimpleText("Name", "Font_25_akbar", width*0.22, height*0.24, Color(100,100,200,math.random(50,200)), TEXT_ALIGN_LEFT)
		-- Frags
		draw.SimpleText("Minion killed", "Font_25_akbar", width*0.45, height*0.24, Color(100,100,200,math.random(50,200)), TEXT_ALIGN_LEFT)
		-- Survived
		--draw.SimpleText("Survived", "Font_25_akbar", width*0.55, height*0.24, Color(100,100,200,math.random(50,200)), TEXT_ALIGN_LEFT)
		-- health
		draw.SimpleText("Health", "Font_25_akbar", width*0.65, height*0.24, Color(100,100,200,math.random(50,200)), TEXT_ALIGN_LEFT)
		-- Actual scoreboard stuff
		
		local allp = player.GetAll()
		all = {}
		sep = {}
		-- We're going to organize
		for i=1,1005 do
			sep[i] = {}
			if team.NumPlayers(i) > 0 then
				for j=1,team.NumPlayers(i) do
					table.insert(sep[i],team.GetPlayers(i)[j])
					if OrganizeType == 1 then -- By kills
						table.sort(sep[i], function (a, b) return a:Frags() > b:Frags() end)
					end	
					if OrganizeType == 3 then -- By health
						table.sort(sep[i], function (a, b) return a:Health() > b:Health() end)
					end	
				end
			end
			table.Add(all,sep[i])
		end
		local heightmul = 0.02
		local baseheight = 0.24 -- height * (baseheight + (heightmul * k))
		local fontheight = 20
		for k,plr in pairs(all) do
			local clr = team.GetColor(plr:Team())
			clr.a = math.random(180,220)
			if plr == LocalPlayer() then
				clr = Color(255,255,0,clr.a)
			end
			-- Name
			draw.SimpleText(plr:Nick(), "Font_"..fontheight.."_akbar", width*0.22, height * (baseheight + (heightmul * k)), clr, TEXT_ALIGN_LEFT)
			-- Frags
			draw.SimpleText(plr:Frags(), "Font_"..fontheight.."_akbar", width*0.49, height * (baseheight + (heightmul * k)), clr, TEXT_ALIGN_CENTER)
			-- Survived
			-- This will be kind of iffy
			local ROUND_TIME = GetGlobalInt("rtime")
			--if plr:GetNWFloat("diedat") > 0 then -- If we have died, show it
				-- We'll use 180 seconds (3 min) as our diedat, and 900 seconds (15 min) as our ROUND_TIME
				--local survive = ROUND_TIME - (plr:GetNWFloat("diedat") - plr:GetNWFloat("spawnedat")) -- 15min - 3min = 12min
				--local survive = (ROUND_TIME) - survive --  divide rt by 60 to get the minutes out of it. 15 minutes - 12 minutes = 3 minutes
				--draw.SimpleText(string.ToMinutesSeconds(survive), "Font_"..fontheight.."_akbar", width*0.58, height * (baseheight + (heightmul * k)), clr, TEXT_ALIGN_CENTER)
			--else -- Otherwise, just show our current time
				--local survive = ROUND_TIME - (CurTime() - plr:GetNWFloat("spawnedat"))
				--local survive = (ROUND_TIME) - survive
				--draw.SimpleText(string.ToMinutesSeconds(survive), "Font_"..fontheight.."_akbar", width*0.58, height * (baseheight + (heightmul * k)), clr, TEXT_ALIGN_CENTER)
			--end
			-- Health
			draw.SimpleText(plr:Health(), "Font_"..fontheight.."_akbar", width*0.675, height * (baseheight + (heightmul * k)), clr, TEXT_ALIGN_CENTER)
		end
	end
	// Do nothing (We're vgui'd up)
	
end

